﻿// Copyright (c)  MOKEYISH. All rights reserved.
// Licensed under the MIT License, See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace NSQL.TypeMapping
{
    [DebuggerDisplay("{" + nameof(TypeName) + "}")]
    public class TypeMap
    {
        private static readonly List<TypeMap> List = new List<TypeMap>();
        public static readonly IReadOnlyList<TypeMap> Types = List.AsReadOnly();
        
        public readonly string TypeName;
        
        /// <summary>
        /// A set of <see cref="DbType"/>s that correspond to this type.
        /// </summary>
        public readonly DbType[] DbTypes;
        
        /// <summary>
        /// A set of CLR types that correspond to this type.
        /// </summary>
        public readonly Type[] ClrTypes;

        /// <summary>
        /// Determines what is returned when this mapping
        /// is used.
        /// </summary>
        public readonly DbType? InferredDbType;
        


        public TypeMap(string typeName, DbType[] dbTypes, Type[] clrTypes, DbType? inferredDbType)
        {
            TypeName = typeName;
            DbTypes = dbTypes ?? EmptyDbTypes;
            ClrTypes = clrTypes ?? EmptyClrTypes;
            InferredDbType = inferredDbType;
            List.Add(this);
        }
        
        public override string ToString() => $"{TypeName}";

        private static readonly DbType[] EmptyDbTypes = Array.Empty<DbType>();
        private static readonly Type[] EmptyClrTypes = Array.Empty<Type>();

    }
}